# -*- coding: utf-8 -*-
'''
@File: utils.py
@Time: 2023/5/7 14:27
@Author: sunda
'''
from app.models import User,Holiday,DictType,DictItem
from app import db

def get_year(year=None):
    '''
    根据年份查询
    '''
    return Holiday.query.filter_by(year=int(year)).all()

def get_area(area=None):
    '''
    根据地区查询
    '''
    return Holiday.query.filter_by(area=area).all()

def get_holiday(year=None,area=None):
    '''
    根据年份 地区查询
    '''
    holidays= Holiday.query.filter_by(year=int(year),area=area).all()
    # holiday_list=[]
    # for i in range(len(holiday)):
    #     holiday_list.append([holiday[i].year,holiday[i].date,holiday[i].area,holiday[i].note])
    # print([[holiday.year,holiday.date,holiday.area,holiday.note] for holiday in holidays])
    # return holiday_list
    return [[holiday.year,holiday.date,holiday.area,holiday.note] for holiday in holidays]

def get_dict_type():
    '''查询数据字典主表'''
    configs=DictType.query.all()
    print(configs)
    return [[config.type_id,config.key,config.value,config.desc,config.status] for config in configs]

def get_dict_type_by_typeid(type_id):
    '''查询数据字典主表'''
    config=DictType.query.filter_by(type_id=int(type_id)).first()
    return config

def get_dict_item(type_id):
    '''查询数据字典从表'''
    configs=DictItem.query.filter_by(type_id=int(type_id)).all()
    return [[config.id,config.type_id,config.sort_id,config.key,config.value,config.desc,config.status] for config in configs]

def dict_add(data):
    try:
        db.session.add(data)
        db.session.commit()
        return True
    except Exception:
        db.session.rollback()
        return False

def dict_type_update(type_id,data):
    try:
        DictType.query.filter_by(type_id=int(type_id)).update(data)
        db.session.commit()
        return True
    except Exception:
        db.session.rollback()
        return False

def dict_type_delete(type_id):
    try:
        DictType.query.filter_by(type_id=int(type_id)).delete()
        db.session.commit()
        return True
    except Exception:
        db.session.rollback()
        return False